//package com.ldgroup.demo.thymeleaf_bootstrap.controller;
package com.javayz.fileuploadanddownload.controller;
//package com.javayz.fileuploadanddownload.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @Description
 * @Author by mocar小师兄
 * @Date 2020/4/14
 **/
@RestController
@RequestMapping("/file")
public class UploadFileController {
    @PostMapping("/uploadFile")
    public String upload(@RequestParam("static/file") MultipartFile file){
        if (file==null){
            return "upload false";
        }
        try {
            String filename = file.getOriginalFilename();
            String extFileName = filename.substring(filename.lastIndexOf("." ) +1,filename.length());
            System.out.println("文件名:\t"+filename);
            System.out.println("后缀名:\t"+extFileName);

            //上传到本地,模拟上传到fastdfs文件服务器
            String filePath= "d:\\frRJ\\";
            String path = filePath + filename;
            //文件存储路径
            File dest = new File(path);
            if (!dest.getParentFile().exists()){
                dest.getParentFile().mkdir();
            }
            file.transferTo(dest);
            return "upload success";
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "upload false";
    }

}